<template>
  <div>
    <validate-form @form-submit="onFormSubmit">
      <div class="mb-3">
        <label class="form-label">邮箱地址</label>
        <validate-input
          :rules="emailRules"
          v-model="emailVal"
          placeholder="请输入邮箱地址"
          type="text"
          ref="inputRef"
        ></validate-input>
      </div>
      <div class="mb-3">
        <label class="form-label">密码</label>
        <validate-input
          :rules="passwordRules"
          v-model="passwordVal"
          placeholder="请输入密码"
          type="password"
        ></validate-input>
      </div>
      <!-- <template #submit>
        <span class="btn btn-danger">登录</span>
      </template> -->
    </validate-form>

    <uploader />
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, ref } from "vue";
import { useStore } from "vuex";
import validateInput from "../components/ValidateInput.vue";
import { RulesProp } from "../components/ValidateInput.vue";
import ValidateForm from "../components/ValidateForm.vue";
import Uploader from "../components/Uploader.vue";
import { useRouter } from "vue-router";
export default defineComponent({
  components: { validateInput, ValidateForm, Uploader },
  setup() {
    const router = useRouter();
    const inputRef = ref<any>();
    const store = useStore();
    const emailRules: RulesProp = [
      {
        type: "required",
        message: "电子邮箱地址不能为空"
      },
      {
        type: "email",
        message: "请输入正确的电子邮箱格式"
      }
    ];
    const emailVal = ref("");
    const emailRef = reactive({
      val: "",
      error: false,
      message: ""
    });
    const validateEmail = () => {
      if (emailRef.val.trim() === "") {
        emailRef.error = true;
        emailRef.message = "can not be empty";
      }
    };
    const passwordVal = ref("");
    const passwordRules: RulesProp = [
      {
        type: "required",
        message: "密码不能为空"
      }
    ];
    const onFormSubmit = (result: boolean) => {
      // console.log(inputRef.value.validateInput());
      if (result) {
        router.push("/");
        store.commit("login");
      }
    };
    return {
      emailRef,
      validateEmail,
      emailRules,
      emailVal,
      passwordVal,
      passwordRules,
      onFormSubmit,
      inputRef
    };
  }
});
</script>

<style scoped></style>
